//
//  comicMenuViewController.m
//  动漫之家
//
//  Created by 王少泽 on 16/12/15.
//  Copyright © 2016年 王少泽. All rights reserved.
//

#import "comicMenuViewController.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>


#import "comicInfoTableViewCell.h"
#import "comicChapterTableViewCell.h"
#import "comicCommentTableViewCell.h"
#import "ChapterCollectionViewCell.h"

#import "XZBaseNetwork.h"




@interface comicMenuViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation comicMenuViewController
{
    UITableView* _comicShowInfo;
    NSArray* _dataSource;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    CGRect frame = _comicShowInfo.tableHeaderView.frame;
    frame.size.height = 1;
    UIView* headView = [[UIView alloc]initWithFrame:frame];
    [_comicShowInfo setTableHeaderView:headView];
    
    
    //请求漫画列表数据
    [[XZBaseNetwork shareInstance] startWithGetURL:@"http://japi.juhe.cn/comic/chapter" params:
  @{@"comicName":self.nameTitle,
    @"skip":@"",
    @"key":@"e4c2c6f92b486c97e01328b1bc54c913"} success:^(id success) {
        
        _dataSource = [[success objectForKey:@"result"] objectForKey:@"chapterList"];
        NSLog(@"_dataSourrrrrce:%@",_dataSource);
        
    } failure:^(id failure) {
        
    }];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self lazyMod];
    
    [self.view addSubview:_comicShowInfo];
}
#pragma mark 懒加载模式
- (UITableView* )lazyMod
{
    if (_comicShowInfo == nil) {
        _comicShowInfo = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _comicShowInfo.dataSource = self;
        _comicShowInfo.delegate = self;
        
        [_comicShowInfo registerNib:[UINib nibWithNibName:NSStringFromClass([comicInfoTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"topCell"];
        
        [_comicShowInfo registerNib:[UINib nibWithNibName:NSStringFromClass([comicChapterTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"midCell"];
        
        [_comicShowInfo registerNib:[UINib nibWithNibName:NSStringFromClass([comicCommentTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"bottomCell"];
        
    }
    return _comicShowInfo;
}



#pragma mark 代理方法的实现
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* topCellID = @"topCell";
    static NSString* midCellID = @"midCell";
    static NSString* bottomCellID = @"bottomCell";
    
    comicInfoTableViewCell* topCell = [tableView dequeueReusableCellWithIdentifier:topCellID];
    comicChapterTableViewCell* midCell = [tableView dequeueReusableCellWithIdentifier:midCellID];
    comicCommentTableViewCell* bottomCell = [tableView dequeueReusableCellWithIdentifier:bottomCellID];
    
    
    if (indexPath.section == 0) {
        if (topCell == nil) {
            topCell = [[comicInfoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topCellID];
            
            
        }
        [topCell.coverImage sd_setImageWithURL:self.coverImage];
        topCell.des.text = _comicSynopsis;

        return topCell;
    }
    if (indexPath.section == 1) {
        if (midCell == nil) {
            midCell = [[comicChapterTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:midCellID];
        }
        
        
        
        midCell.readVC = self;
        
        return midCell;
    }
    if (indexPath.section == 2) {
        if (bottomCell == nil) {
            bottomCell = [[comicCommentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:midCellID];
        }
        return bottomCell;
    }
    
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 200;
    }
    if (indexPath.section == 1) {
        return 150;
    }
    else
    {
        return 243;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    else
    {
        return 5;
    }
}

#pragma mark UICollectionView代理方法




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
